//
//  BaseDataModel.m
//  jiurongjk
//
//  Created by cabinzhao on 3/1/17.
//  Copyright © 2017 xuewei. All rights reserved.
//

#import "CBModel.h"
#import <objc/runtime.h>

@implementation CBModel

+ (instancetype)getObjectByDict:(NSDictionary *)dict isAutoToString:(BOOL)isAutoToString {
    id obj = [[[self class] alloc]init];
    NSArray *allKeys = dict.allKeys;
    for (NSString *key in allKeys) {
        id value = dict[key];
        if( !value || [value isKindOfClass:[NSNull class]]) {
            value = @"";
        }
        value = isAutoToString ? [NSString stringWithFormat:@"%@",value] : value;
        [obj setValue:value forKey:key];
    }
    return obj;
}

+ (NSArray *)getArrByNetDicList:(NSArray *)arrNetDic isAutoToString:(BOOL)isAutoToString isAutoToMapped:(BOOL)isAutoToMapped propertyDic:(NSDictionary * __nullable)proDic
{
    NSMutableArray *mu = [NSMutableArray array];
    if ([arrNetDic isKindOfClass:[NSNull class]]) {
        return mu;
    }
    
    NSDictionary *realProDic;
    //如果是自动映射，则自动解析该对象得属性，不使用传入的解析映射
    if (isAutoToMapped) {
        //动态解析此对象的所有属性
        NSArray *arrObjProps = [self getAllProperties];
        NSMutableDictionary *muPropDic = [[NSMutableDictionary alloc]initWithCapacity:20];
        for (NSString *strProp in arrObjProps) {
            [muPropDic setObject:strProp forKey:strProp];
        }
        //如果自动映射，还传入了proDic, 则附加上去
        if (proDic) {
            NSArray *pdAllKeys = proDic.allKeys;
            for (NSString *key in pdAllKeys) {
                [muPropDic setObject:proDic[key] forKey:key];
            }
        }
        realProDic = muPropDic;
    } else { //否则使用传入的对象进行解析
        realProDic = proDic;
    }
    
    for (NSDictionary *netDic in arrNetDic) {
        id obj = [[[self class] alloc]init];
        //是否忽略此条数据self.dicBrand[section]
        BOOL isIgnore = NO;
        
//        if (mainKeys) {
//            //任意主键缺失，过滤数据
//            for (NSString *mainKey in mainKeys) {
//                if ([netDic objectForKey:mainKey] == nil) {
//                    isIgnore = YES;
//                    break;
//                }
//            }
//        }
//
//        //一个副键都不存在，则过滤数据
//        if(isIgnore == NO) {
//            if (auxiliaryKeys) {
//                int auKeysCount = 0;
//                for (NSString *auxiliaryKey in auxiliaryKeys) {
//                    if ([netDic objectForKey:auxiliaryKey]) {
//                        ++auKeysCount;
//                    }
//                }
//                isIgnore = (auKeysCount) ? NO : YES;
//            }
//        }
        
        if (isIgnore == NO) {
            for (NSString *key in realProDic.allKeys) {
                NSString *localProperty = key;
                NSString *netProperty = realProDic[key];
                //
                id value ;
                //如果是字符串，则执行映射取值
                if ([netProperty isKindOfClass:[NSString class]]) {
                    if ([netProperty containsString:@"."]) {
                        NSArray *strArr = [netProperty componentsSeparatedByString:@"."];
                        id od = netDic;
                        for (NSString *dicName in strArr) {
                            if (![od isKindOfClass:[NSDictionary class]]) {
                                od = nil;
                                break;
                            }
                            od = [od objectForKey:dicName];
                        }
                        value = od;
                    } else {
                        if (netDic[netProperty]) {
                            value = netDic[netProperty];
                        }
                    }
                } else {  //否认为代码块，执行代码块回调取值
                    id (^returnBlock)(NSDictionary *);
                    returnBlock = (id (^)(NSDictionary *))netProperty;
                    value = returnBlock(netDic);
                }
                
                if (value) {
                    if ([value isKindOfClass:[NSNull class]]) {
                        value = @"null";
                    }
                    value = isAutoToString ? [NSString stringWithFormat:@"%@",value] : value;
                    [obj setValue:value forKey:localProperty];
                }
            }
            [mu addObject:obj];
        }
        
    }
    return mu;
}

+ (NSArray *)getAllProperties
{
    u_int count;
    objc_property_t *properties  =class_copyPropertyList([self class], &count);
    NSMutableArray *propertiesArray = [NSMutableArray arrayWithCapacity:count];
    for (int i = 0; i<count; i++)
    {
        const char* propertyName =property_getName(properties[i]);
        [propertiesArray addObject: [NSString stringWithUTF8String: propertyName]];
    }
    free(properties);
    return propertiesArray;
}

- (NSArray *)getAllPropertiesByObject
{
    u_int count;
    objc_property_t *properties  =class_copyPropertyList([self class], &count);
    NSMutableArray *propertiesArray = [NSMutableArray arrayWithCapacity:count];
    for (int i = 0; i<count; i++)
    {
        const char* propertyName =property_getName(properties[i]);
        [propertiesArray addObject: [NSString stringWithUTF8String: propertyName]];
    }
    free(properties);
    return propertiesArray;
}

-(BOOL)isEqual:(id)object {
    NSMutableSet *set1 = [NSMutableSet setWithArray:[self getAllPropertiesByObject]];
    NSMutableSet *set2 = [NSMutableSet setWithArray:[object getAllPropertiesByObject]];
    [set1 intersectSet:set2];  //取交集后 set1中为1
    NSArray *arrProps = [set1 allObjects];
    for (NSString *prop in arrProps) {
        if (![[self valueForKey:prop] isEqual:[object valueForKey:prop]]) {
            return NO;
        }
    }
    return YES;
}


@end
